﻿define([], function (Mediator) {

    var channels = {};

    if (!Mediator) {
        Mediator = {};
    }

    Mediator.subscribe = function (channel, subscription, scope) {
        if (!channels[channel]) {
            channels[channel] = [];
        }

        channels[channel].push({
            subscription: subscription,
            scope: scope
        });
    };

    Mediator.publish = function (channel) {
        if (!channels[channel]) {
            return
        };

        var args = [].slice.call(arguments, 1);

        for (var i = 0, l = channels[channel].length; i < l; i++) {
            var reference = channels[channel][i],
                scope = this;

            if (reference.scope) {
                scope = reference.scope;
            }

            reference.subscription.apply(scope, args);
        }
    };

    return Mediator;
});